//
//  API.swift
//  HYOnline
//
//  Created by zhuxuhong on 2017/3/21.
//  Copyright © 2017年 zhuxuhong. All rights reserved.
//

import Foundation
import Alamofire


struct API {
	var method: HTTPMethod
	var parameters: Parameters?
	var action: String
    var encoding: Encoding
	var needsTipper: Bool
	
	init(method: HTTPMethod = .get, parameters: Parameters? = nil, action: String, encoding: Encoding? = nil, needsTipper: Bool = true) {
		self.method = method
		self.parameters = parameters
		self.action = action
		self.needsTipper = needsTipper
		
		if encoding != nil {
			self.encoding = encoding!
		}
		else{
			self.encoding = Encoding.with(method: method)
		}
	}
}

extension API{
	enum Encoding: ParameterEncoding {
		case with(method: HTTPMethod)
		case json
		
		func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
			switch self {
			case .json:
				return try! JSONEncoding.default.encode(urlRequest, with: parameters)
			case .with(let method):
				guard var urlString = urlRequest.urlRequest?.url?.absoluteString else{
					fatalError("[API.Encoding] URL不能为空")
				}
				
				var parameterList = ""
				if let parameters = parameters{
					for (key, value) in parameters{
						parameterList += "\(key)=\(value)&"
					}
					urlString += "?\(parameterList)"
				}
				guard
					let string = urlString.addingPercentEscapes(using: .utf8),
					let url = URL(string: string) else{
					fatalError("[API.Encoding] 不合法的URL")
				}
				return try! URLRequest(url: url, method: method)
			}
		}
	}
}

extension API{
	enum Action: String {
		case gradeList = "external/cat/getLearningStages/"
		case resourceCategoryList = "external/cat/getResourceType/"
        case resourceDetail = "external/resource/resourceView/"
        case resourceAddFavorite = "external/resource/addFavResource/"
        
		enum Student: String {
			case favoriteResourceList = "external/resource/getfavResource/"
			case favoriteQuestionList = "external/resource/getfavResource/ssss"
			case wrongQuestionList = "external/info/student/myWrongQuestion/"
			
			enum Learning: String {
				case fullStars = "external/learningCapacity/analysis/progress/standard"
				case list = "external/learningCapacity/analysis/student/knowledge/"
				case learnList = "external/resource/student/getResourceList/"
				case practiseList = "external/learningCapacity/homework/getExerciseContent/"
				case quizList = "external/learningCapacity/homework/getTestContent/"
				case recommendList = "external/resource/student/getResourceListBysolomon/"
                case weakList = "external/learningCapacity/analysis/student/weaknessAnalysis/"
                case submitAnswer = "external/learningCapacity/homework/saveExamResult"
                case updateProgress = "external/learningCapacity/updateProgress/"
            }
			enum Assignment {
				
			}
            enum Profile{
			}
		}
		enum Teacher: String {
			case login = "ios/teacher/test/iosLogin.do"
			case classList = "external/info/teacher/getClassList/"
			case classStudentList = "external/info/teacher/getClassStudent/"
			case classStudentLearningList = "external/learningCapacity/analysis/teacher/classStudentLearningByCatId/"
			
			enum Learning: String {
				case list = "external/learningCapacity/analysis/teacher/knowledge/"
			}
			enum Assignment: String {
				case listH5 = "mobile/modelTwo/teacher/myHomework.do"
			}
			enum Profile: String {
				case verifyOldPasswordRight = "verifyOldPwd"
				case verifyNewPasswordRepeat = "verifyPwdRepeat"
				case modifyPassword = "modifyPwd"
				case modifyInformation = "modifyUserInfo"
				case favoriteResource = "getfavResource"
				case messageListH5 = "/mobile/teacher/discuss/list.do"
				
				var value: String
				{
					switch self {
					case .verifyOldPasswordRight, 
					     .verifyNewPasswordRepeat, 
					     .modifyPassword,
					     .modifyInformation:
						return "external/info/\(rawValue)/"
					case .favoriteResource:
						return "external/resource/\(rawValue)/"
					default: 
						return rawValue;
					}
				}
			}
		}
	}
}

extension API {
	public var host: String{
		return "120.55.181.205:8080" //"172.19.36.180:8080"
	}
	public var path: String{
		return "external"
	}
	public var url: String{
		return "http://\(host)/\(action)"
	}
}
